package com.qf.controller;

import com.google.common.collect.Maps;
import com.qf.ann.ResponseResult;
import com.qf.dto.ApiResult;
import com.qf.entity.request.User;
import com.qf.enums.ApiEnum;
import com.qf.exception.GlobalException;
import com.qf.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "hello和login接口")
@ResponseResult
public class HelloController {

    static Map<String,String> map=new HashMap<>();

    static{
        map.put("张三","123456");
    }



    //打印hello world
    @RequestMapping("/hello")
    public User print() throws FileNotFoundException {
        /*
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(200);
        apiResult.setMessage("成功");
        apiResult.setData("hello world");
        return apiResult;
        return new ApiResult(200,"成功","hello world");
        return new ApiResult(ApiEnum.SUCCESS,"hello world");
         */
//        List<String> list = new ArrayList<>();
//        list.get(0);
//        int i=1/0;

//        String s=null;
//        s.toString();

//        File file=new File("abc");
//        InputStream inputStream=new FileInputStream(file);

//        throw new GlobalException(ApiEnum.USER_NAME_OR_PWD_ERROR);
//        return R.fail();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        return user;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录接口")
    public ApiResult login(@RequestBody User user){
        String mapPassword = map.get(user.getUsername());

        if(mapPassword==null||"".equals(mapPassword)){
            throw new GlobalException(ApiEnum.USER_NAME_OR_PWD_ERROR);
        }

        if(!mapPassword.equals(user.getPassword())){
            throw new GlobalException(ApiEnum.USER_NAME_OR_PWD_ERROR);
        }
        return R.ok();
    }
}
